package com.weu.service.impl;

import com.weu.bean.Result;
import com.weu.bean.sysUser;
import com.weu.mapper.UserMapper;
import com.weu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
//    @Autowired
//    PasswordEncoder passwordEncoder;
    @Override
    public sysUser findUserByName(String userName) {
        return userMapper.findUserByName(userName);
    }

    @Override
    public Result register(sysUser user) {
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            sysUser existUser = userMapper.findUserByName(user.getUsername());
            if(existUser != null){
                //如果用户名已存在
                result.setMsg("用户名已存在");

            }else{
                userMapper.register(user);
                //System.out.println(user.getId());
                result.setMsg("注册成功");
                result.setSuccess(true);
                result.setDetail(user);
            }
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

}
